<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class EmailSettingForm extends CFormModel
{

	public  $enabledEmail;	public  $enabledMailFun;	public  $smtpServer;	public  $port;	public  $sender;	public  $checkAuth;	public  $name;	public  $passwd;	public  $receiver;	public  $action;	public  $message;    
	public function myErrorHandler($errno, $errstr, $errfile, $errline) {
		if(!$this->hasErrors()){
			$this->addError("enabledEmail", "邮件设置不正确<br>1、mail函数模式在Windows下默认不支持;<br>2、SMTP注意到发件人与验证用户名一般情况一样;");
		}
		return true;
	}
	
	public function myExceptionHandler() {
		if(!$this->hasErrors()){
			$this->addError("enabledEmail", "邮件设置不正确<br>1、mail函数模式在Windows下默认不支持;<br>2、SMTP注意到发件人与验证用户名一般情况一样;");
		}
		return true;
	}
	public function init(){
		set_error_handler( array( &$this, 'myErrorHandler' ) );
		set_exception_handler( array( &$this, 'myExceptionHandler' ) );
		
		$this->enabledEmail = "1";
		$this->enabledMailFun = "1";
		$this->checkAuth = "1";
		$this->port = "25";
		$model = new Option();
		$data = $model->getMailSettingOptions();
		foreach ($data as $index=>$item){
			$key = $item["option_name"];
			$value = $item["option_value"];
			if($key=="mail_enabled_email"){
				$this->enabledEmail = $value;
			}
			if($key=="mail_enabled_mail_fun"){
				$this->enabledMailFun = $value;
			}
			if($key=="mail_smtp_server"){
				$this->smtpServer = $value;
			}
			if($key=="mail_port"){
				$this->port = $value;
			}
			if($key=="mail_sender"){
				$this->sender = $value;
			}
			if($key=="mail_check_auth"){
				$this->checkAuth = $value;
			}
			if($key=="mail_name"){
				$this->name = $value;
			}
			if($key=="mail_password"){
				$this->passwd = CSecurity::decrypt($value);
			}
		}

	}
	public function rules()
	{
		return array(
		array('receiver', 'required'),
		array('enabledEmail', 'length', 'max'=>128),
		array('enabledMailFun', 'length', 'max'=>128),
		array('smtpServer', 'length', 'max'=>128),
		array('port', 'length', 'max'=>128),
		array('sender', 'length', 'max'=>128),
		array('checkAuth', 'length', 'max'=>128),
		array('name', 'length', 'max'=>128),
		array('passwd', 'length', 'max'=>128),
		array('action', 'length', 'max'=>128),
		array('receiver','email'),
		);
		return $valid;
	}
	public function attributeLabels()
	{ 		return  array(
            'receiver'=>'收件人地址',
            'enabledEmail'=>'开启邮件发送功能',
            'enabledMailFun'=>'邮件发送方式',
            'smtpServer'=>'SMTP服务器',
            'port'=>'SMTP端口',
			'sender'=>'发信人地址',
		    'checkAuth'=>'SMTP用户身份验证',
		    'name'=>'验证用户名',
		    'passwd'=>'验证密码',
		);
	}
	
	private function _validateBaseSetting(){
		$validate = true;
		if($this->enabledEmail=="1" && $this->enabledMailFun=="0"){			if(strlen($this->smtpServer)==0){
				$this->addError("smtpServer", "SMTP服务器不能为空");
				$validate = false;
			}
			if(strlen($this->port)==0||is_numeric($this->port)==false){
				$this->addError("port", "端口必须为数字");
				$validate = false;
			}
			if($this->checkAuth=="1"){
				if(strlen($this->name)==0){
					$this->addError("name", "验证用户名为空");
					$validate = false;
				}
				if(strlen($this->passwd)==0){
					$this->addError("passwd", "验证密码为空");
					$validate = false;
				}
			}
		}
		return $validate;
	}
	
	public function  testEmail(){
		if($this->validate() && $this->_validateBaseSetting()){
			$result = CEmailSender::sendMail($this->receiver, "测试邮件", "测试邮件",$this);
			if(is_bool($result) && $result){
				$this->message = "邮件发送成功";
				$this->_save();
				return true;
			}else{
				if(!$this->hasErrors()){
					$this->addError("receiver",$result);
				}
			}
		}
		return false;
	}
	
	private function _save(){
		$this->_saveOption("mail_enabled_email",$this->enabledEmail);
		$this->_saveOption("mail_enabled_mail_fun",$this->enabledMailFun);
		$this->_saveOption("mail_smtp_server",$this->smtpServer);
		$this->_saveOption("mail_port",$this->port);
		$this->_saveOption("mail_sender",$this->sender);
		$this->_saveOption("mail_check_auth",$this->checkAuth);
		$this->_saveOption("mail_name",$this->name);
		$this->_saveOption("mail_password",CSecurity::encrypt($this->passwd));
		CEmailSender::resetConfig();		$this->message = "保存成功";
	}
	
	private  function _saveOption($key,$value){
		$option = Option::model()->find("option_name=?",array($key));
		if(!isset($option)){
			$option = new Option();
			$option["option_name"]=$key;
		}
		$option["option_value"]=$value;
		$option->save();
	}

}